package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
@Api(tags = "地址相关接口")
public class AddressController {

    @Autowired
    private AddressService addressService;


    @PostMapping
    @ApiOperation("新增地址")
    public Result addAddress(@RequestBody AddressBook addressBook){

        log.info("新增地址信息：{}",addressBook);
        addressService.addAddress(addressBook);
        return Result.success();
    }


    @GetMapping("/list")
    @ApiOperation("查询当前用户所有地址信息")
    public Result<List<AddressBook>> getAllAddress(){
        List<AddressBook> list =  addressService.getAllAddress();
        return Result.success(list);
    }


    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook){

        log.info("设置默认地址，{}",addressBook);
        addressService.setDefaultAddress(addressBook);

        return Result.success();
    }

    /**根据id查询地址,修改回显
     *
     * @return
     */

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> echo(@PathVariable Long id){

        AddressBook addressBook = addressService.echo(id);
        return Result.success(addressBook);

    }


    /**修改地址信息
     *
     */

    @PutMapping
    @ApiOperation("修改信息")
    public Result update(@RequestBody AddressBook addressBook){
        addressService.update(addressBook);
        return Result.success();
    }
    /**根据id 删除地址
     *
     *
     */

    @DeleteMapping
    @ApiOperation("删除地址信息")
    public Result deleteById(Long id){
        addressService.deleteById(id);
        return Result.success();

    }

    /**查询默认地址
     *
     */

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = addressService.getDefault();
        return Result.success(addressBook);
    }

}
